#include "CASsiUARTDriverE.h"

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CASsiUARTDriver::EDROOMTopContext::EDROOMTopContext (CASsiUARTDriver &act, bool_t & EDROOMpVarVUARTError ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,IrqUART(EDROOMcomponent.IrqUART)
	,SsiFromComponent(EDROOMcomponent.SsiFromComponent)
	, VUARTError ( EDROOMpVarVUARTError )
 {

}

CASsiUARTDriver::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,IrqUART(contex.IrqUART)
	,SsiFromComponent(contex.SsiFromComponent)
	, VUARTError ( contex.VUARTError )
 {

}

	// EDROOMSearchContextTrans********************************************

bool CASsiUARTDriver::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CASsiUARTDriver::EDROOMTopContext::FG()

int8_t leon3_print_string(char* str)
{
	uint8_t i=0;
 
	while (str[i]!='\0')
	{
		if (leon3_putchar(str[i]))
		{
			return 0;
		}
		i++;
	}
	return 1;
}
 
 



void	CASsiUARTDriver::EDROOMTopContext::FSendToSensor()

{
	uint8_t i=0;
	CDFrame & varCDFrame = *(CDFrame *)Msg->data;
	VUARTError=false;
 
	for (i=0;i<48;i++)
	{
		if (uart_dev_putchar(varCDFrame.dataFrame[i]))
		{
			VUARTError=true;
			return;
		}
	}
 
}



void	CASsiUARTDriver::EDROOMTopContext::FInit()

{
 
}



void	CASsiUARTDriver::EDROOMTopContext::FGetCharFromUART()

{
 
}



	//*****Pools



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CASsiUARTDriver::EDROOMTopState::EDROOMTopState (CASsiUARTDriver &act ):
	  EDROOMTopContext( act , VUARTError )
	, VUARTError ( false )
{

}

	// EDROOMBehaviour**********************************************

void CASsiUARTDriver::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				edroomNextState = Listening;
				break;
			case(SendToUART):
				FSendToSensor();
				edroomNextState = Listening;
				break;
			case(ReceiveFromUART):
				FGetCharFromUART();
				edroomNextState = Listening;
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Listening):
				edroomCurrentTrans=EDROOMListeningllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CASsiUARTDriver::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CASsiUARTDriver::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Listening

	// ***********************************************************************



TEDROOMTransId CASsiUARTDriver::EDROOMTopState::EDROOMListeningllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (SDataFrame): 

				 if ( *Msg->GetPInterface() == SsiFromComponent) {

					edroomCurrentTrans.localId= SendToUART;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (DataReceived): 

				 if ( *Msg->GetPInterface() == IrqUART) {

					edroomCurrentTrans.localId= ReceiveFromUART;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



